<?php
/* PERILAMVANONTAI OI SUNRATISEIS POU EXOUN NA KANOUN ME TIN SUNDESI KAI APOSUNDESI 
TWN XRISTWN KAI TIS VASIS DEDOMENWN */



/*
	sundesi kai epilogi vasis dedomenwn
	kaleite stin arxi kathe selidas pou diaxeireizetai periexomena
	tis vasis dedomenwn
*/
function db_connect()
{
	//ΣΥΝΔΕΣΗ ΜΕ ΤΗΝ MYSQL
	$connection = mysql_connect("localhost","root","");
	if(!$connection){die("Database connection failed:".mysql_error());}
	//ΕΠΙΛΟΓΗ ΤΗΣ ΒΑΣΗΣ
	$db_select=mysql_select_db("rental_store",$connection);
	if(!$db_select){die("Database selection failed".mysql_error());}
	$GLOBALS['connection']=$connection;	
}


//ΑΠΟΣΥΝΔΕΣΗ ΑΠΟ ΤΗΝ ΒΑΣΗ ΔΕΔΟΜΕΝΩΝ
function db_disconnect()
{
	mysql_close($GLOBALS['connection']);
}

/*
	Login, elegxos username kai password
	kaleite stin selida login_result.php
*/
function login(){
	
	 //ΣΥΝΔΕΣΗ ΜΕ ΤΗΝ MYSQL
	$connection = mysql_connect("localhost","root","123");
	if(!$connection){die("Database connection failed:".mysql_error());}
	//ΕΠΙΛΟΓΗ ΤΗΣ ΒΑΣΗΣ
	$db_select=mysql_select_db("rental_store",$connection);
	if(!$db_select){die("Database selection failed".mysql_error());}	
	
	$login=FALSE;
	$username=$_POST['username'];
	$password=$_POST['password'];
	if (preg_match ("/[&'<>%\*\,\.]/i", $username) || preg_match ("/[&'<>%\*\,\.]/i", $password))
		echo("<h1>You cannot use the following characters:[&'<>%\*\,\.]|</h1>");
	if(empty($username)||empty($password))
	{
		exit("<h1>Empty fields are not acceptable!</h1>");	
	}
	
		$data=mysql_query("SELECT * FROM user");
		while($info=mysql_fetch_array($data))
		{
			if($username==$info['username'] && $password==$info['password'])
			{
				session_start();
				$_SESSION['log_status']=$login;
				$_SESSION['last_name']=$info['last_name'];
				$_SESSION['username']=$info['username'];
				$_SESSION['password']=$info['password'];
				$login=true;
				$_SESSION['log_status']=$login;
				echo"<h1>Logging in...<h1>";
			}
		}
		if($login==false)
			{
				echo"<h1>"."Wrong Input" . "</h1>" . "<br>";	
			}
}

/*logout
  kaleite stin selida logout.php
*/
	function logout(){
		session_start();
		session_destroy();
	}

?>